const { app, BrowserWindow, Menu, ipcMain } = require('electron')
const isDev = require('electron-is-dev');
const Store = require('electron-store');
const AppWindow = require('./AppWindow')
const menuTemplete = require('./src/templeteMenu')
const path =  require('path');
let mainWindow;
let settingWindow;

app.on('ready',() => {
  Store.initRenderer();

  // maninwindow   //生产环境直接引用打包后的index.html文件
  const urlLocation = isDev ? 'http://localhost:3000' : `file://${path.join(__dirname,'./build/index.html')}`;
  mainWindow = new AppWindow({
    width: 1024,
    height: 680,
  }, urlLocation)
  mainWindow.on('closed',() => {
    mainWindow = null
  })
  //settingwindow
  ipcMain.on('open-setting-window',() => {
    const settingWindowLocationUrl = `file:${path.resolve(__dirname, './setting/setting.html')}`
    settingWindow = new AppWindow({
      width: 450,
      height: 400
    }, settingWindowLocationUrl)
    settingWindow.on('closed', () => {
      settingWindow = null;
    })
  })

  //build menu
  const menu = Menu.buildFromTemplate(menuTemplete)
  Menu.setApplicationMenu(menu)
})

app.on('close',() => {
  mainWindow = null;
})